import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
#解决中文显示问题
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False

#第一题
path='.\source.jpg'
img = cv.imread(path)
img=cv.cvtColor(img,cv.COLOR_RGB2BGR)
img1 = cv.cvtColor(img, cv.COLOR_RGB2GRAY)

plt.subplot(1,2,1); plt.imshow(img); plt.axis('off'); plt.title('原图')
plt.subplot(1,2,2); plt.imshow(img1,cmap='gray');plt.axis('off'); plt.title('灰度图')

plt.savefig('01.png',dpi=500)
plt.show()

# 第二题
target1=200
target2=75
long=255
sub_long=41
background=1

data=np.full((255,255),background,dtype=int)

new_im0 = Image.fromarray(data.astype(int))

for i in range(len(data)):
    for j in range(len(data[i])):
        if i>=108 and i<=148 and j>=108 and j<=148:
            data[i][j]=target1

new_im1 = Image.fromarray(data.astype(int))

for i in range(len(data)):
    for j in range(len(data[i])):
        if i>=108 and i<=148 and j>=108 and j<=148:
            data[i][j]=target2
new_im2 = Image.fromarray(data.astype(int))
plt.subplot(1,3,1); plt.imshow(new_im0); plt.title('原图')
plt.subplot(1,3,2); plt.imshow(new_im1); plt.title('▲I/I = 200/255')
plt.subplot(1,3,3); plt.imshow(new_im2); plt.title('▲I/I =75/255')


plt.savefig('02.png',dpi=500)
plt.show()
